var recAppointmentCtrl = myApp.controller('recAppointmentCtrl', ['$scope', '$rootScope', 'NgTableParams', function($scope, $rootScope, NgTableParams) {
	$rootScope.firstType = Tools.permissionList[0].permissionId;
	$rootScope.secondType = Tools.permissionList[0].sonPermission[6].permissionId;
	
	$scope.form = {
		title: ''
	};
	$scope.form.inquiry = {
		curPage: 1,
		inquiryKeyword: "",
		pageList:  [],
		curPage: 1,
		totalPage: 1,
		totalRow: 0
	};
	$scope.form.trace = {
		curPage: 1,
		pageList:  [],
		curPage: 1,
		totalPage: 1,
		totalRow: 0
	};
	$scope.select = {
		owner: {},
		classTypeId: {},
		status: {}
	};

	$scope.delItem = {};
	$scope.traceItem = {};
	$scope.userList = [];
	$scope.classTypeList = [];

	// 分页
	$scope.pageList = [];
	$scope.curPage = 1;
	$scope.totalPage = 1;
	$scope.totalRow = 0;
	var PAGESIZE = 10;

	// 提示弹层
	$scope.msg = "";
	$scope.visible = false;

	// 获取预约列表
	$scope.getAppointmentList = function(curPage) {
		var data = {
			curPage: curPage,
			pagesize: PAGESIZE,
			classTypeId: $scope.filter.classTypeId,
			keyword: $scope.filter.keyword,
			owner: $scope.filter.owner,
			status:	$scope.filter.status,
			visitTimeEnd: $scope.filter.visitTimeEnd,
			visitTimeStart: $scope.filter.visitTimeStart
		}
		commonSrv.getAppointmentList(data, function(result) {
			var classTypeList = result.data.classTypeList;
			var purposeList = result.data.purposeList;
			var statusList = result.data.statusList;
			var userList = result.data.userList;
			var appointmentList = result.data.appointmentList;

			for(var i in appointmentList) {
                // 意向班级
                appointmentList[i].inquiry.classTypeId = Tools.getItemById(appointmentList[i].inquiry.classTypeId, 'id', classTypeList);
                // 邀约人
                appointmentList[i].owner = Tools.getItemById(appointmentList[i].owner, 'userId', userList);
                // 状态
                appointmentList[i].status = Tools.getItemById(appointmentList[i].status, 'id', statusList);
                // 邀约目的
                appointmentList[i].purposeId = Tools.getItemById(appointmentList[i].purposeId, 'id', purposeList);
            }
			$scope.appointmentList = new NgTableParams({count: PAGESIZE}, { counts: [],  dataset: appointmentList});

			// 邀约人下拉列表
			if($scope.userList.length == 0) {
				$scope.userList = userList;
			}
			// 意向班型下拉列表
			if($scope.classTypeList.length == 0) {
				$scope.classTypeList = classTypeList;
			}
			// 获取分页信息
			var paginate = result.paginate;
			$scope.totalPage = paginate.totalPage;
			$scope.totalRow = paginate.totalRow;
			// 设置分页
			$scope.setPage(curPage);
		}, function(result) {
			if(result.status == 1) {
				alert(result.msg);
			}
		}, function() {
			$scope.$apply();
		});
	};

	// 设置分页
	$scope.setPage = function(curPage, type) {
		if(type == 'inquiry') {
			var newPageList = [];
			if (curPage < 1 || curPage > $scope.form.inquiry.totalPage) {
				alert("设置分页失败");
				return;
			}
			var SHOWSIZE = 5;
			// 总页数小于SHOWSIZE
			if($scope.form.inquiry.totalPage <= SHOWSIZE) {
				for(var i=0; i<$scope.form.inquiry.totalPage; i++) {
					newPageList.push(i+1);
				}
			// 总页数大于等于SHOWSIZE
			} else {
				if(curPage > 2) {
					// 最后5页
					if(($scope.form.inquiry.totalPage - curPage) < 2) {
						for(var i = ($scope.form.inquiry.totalPage - 5); i < $scope.form.inquiry.totalPage; i++) {
							newPageList.push(i + 1);
						}
					} else {
						for (var i = (curPage - 3); i < ((curPage + 2) > $scope.form.inquiry.totalPage ? $scope.form.inquiry.totalPage : (curPage + 2)); i++) {
	                        newPageList.push(i + 1);
	                    }
					}
				} else {
					for(var i=0; i<SHOWSIZE; i++) {
						newPageList.push(i + 1);
					}
				}
			}
	        $scope.form.inquiry.pageList = newPageList;
	        $scope.form.inquiry.curPage = curPage;
		} else if(type == 'trace') {
			var newPageList = [];
			if (curPage < 1 || curPage > $scope.form.trace.totalPage) {
				alert("设置分页失败");
				return;
			}
			var SHOWSIZE = 5;
			// 总页数小于SHOWSIZE
			if($scope.form.trace.totalPage <= SHOWSIZE) {
				for(var i=0; i<$scope.form.trace.totalPage; i++) {
					newPageList.push(i+1);
				}
			// 总页数大于等于SHOWSIZE
			} else {
				if(curPage > 2) {
					// 最后5页
					if(($scope.form.trace.totalPage - curPage) < 2) {
						for(var i = ($scope.form.trace.totalPage - 5); i < $scope.form.trace.totalPage; i++) {
							newPageList.push(i + 1);
						}
					} else {
						for (var i = (curPage - 3); i < ((curPage + 2) > $scope.form.trace.totalPage ? $scope.form.trace.totalPage : (curPage + 2)); i++) {
	                        newPageList.push(i + 1);
	                    }
					}
				} else {
					for(var i=0; i<SHOWSIZE; i++) {
						newPageList.push(i + 1);
					}
				}
			}
	        $scope.form.trace.pageList = newPageList;
	        $scope.form.trace.curPage = curPage;
		} else {
			var newPageList = [];
			if (curPage < 1 || curPage > $scope.totalPage) {
				alert("设置分页失败");
				return;
			}
			var SHOWSIZE = 5;
			// 总页数小于SHOWSIZE
			if($scope.totalPage <= SHOWSIZE) {
				for(var i=0; i<$scope.totalPage; i++) {
					newPageList.push(i+1);
				}
			// 总页数大于等于SHOWSIZE
			} else {
				if(curPage > 2) {
					// 最后5页
					if(($scope.totalPage - curPage) < 2) {
						for(var i = ($scope.totalPage - 5); i < $scope.totalPage; i++) {
							newPageList.push(i + 1);
						}
					} else {
						for (var i = (curPage - 3); i < ((curPage + 2) > $scope.totalPage ? $scope.totalPage : (curPage + 2)); i++) {
	                        newPageList.push(i + 1);
	                    }
					}
				} else {
					for(var i=0; i<SHOWSIZE; i++) {
						newPageList.push(i + 1);
					}
				}
			}
	        $scope.pageList = newPageList;
	        $scope.curPage = curPage;
	    }
	};
	//设置当前选中页样式
    $scope.isActivePage = function (page, type) {
    	if(type == 'inquiry') {
    		return $scope.form.inquiry.curPage == page
    	} else if(type == 'trace') {
    		return $scope.form.trace.curPage == page
    	}else {
    		return $scope.curPage == page;
    	}
    };
    //上一页
    $scope.previous = function (type) {
    	if(type == 'inquiry') {
    		if($scope.form.inquiry.curPage > 1) {
	        	$scope.getInquiryList($scope.form.inquiry.curPage - 1);
	        }
    	} else if (type == 'trace') {
    		if($scope.form.trace.curPage > 1) {
	        	$scope.getTraceList($scope.form.trace.curPage - 1);
	        }
    	}else {
    		if($scope.curPage > 1) {
	        	$scope.getAppointmentList($scope.curPage - 1);
	        }
    	}
    }
    //下一页
    $scope.next = function (type) {
    	if(type == 'inquiry') {
    		if($scope.form.inquiry.curPage < $scope.form.inquiry.totalPage) {
	        	$scope.getInquiryList($scope.form.inquiry.curPage + 1);
	        }
    	} else if(type == 'trace') {
    		if($scope.form.trace.curPage < $scope.form.trace.totalPage) {
	        	$scope.getTraceList($scope.form.trace.curPage + 1);
	        }
    	} else {
    		if($scope.curPage < $scope.totalPage) {
	        	$scope.getAppointmentList($scope.curPage + 1);
	        }
    	}
    };
    // 打开弹层
	$scope.showModel = function() {
		$("#formModel").modal({backdrop: 'static', keyboard: false, show: true});
	};
	// 关闭弹层
	$scope.hideModel = function() {
		$("#formModel").modal('hide');
	};
	// 提交表单，做提交表单验证，以及需要执行的方法
	$scope.validate = function(type) {
		if(type == 'trace') {
			var flag = false;
	    	$scope.watchTrackDate();
	    	$scope.watchPlanNextTrackDate();
	    	$scope.watchNotes();
	    	$scope.watchPurpose();

	    	// 验证
	    	if(!Tools.reg.isNull($scope.traceItem.warn.notes) || !Tools.reg.isNull($scope.traceItem.warn.purpose) || !Tools.reg.isNull($scope.traceItem.warn.planNextTrackDate) || !Tools.reg.isNull($scope.traceItem.warn.trackDate)) {
	    		flag = false;
	    	} else {
	    		flag = true;
	    	}
	    	return flag;
		} else {
			var flag = false;
			$scope.watchInquiryId();
			$scope.watchOwner();
	    	// 验证
	    	if(!Tools.reg.isNull($scope.form.item.warn.inquiryId) || !Tools.reg.isNull($scope.form.item.warn.owner)) {
	    		flag = false;
	    	} else {
	    		flag = true;
	    	}
	    	return flag;
    	}
	}
    // 筛选日期范围
    $scope.selectTrackDate = function () {
    	$scope.filter.visitTimeStart = moment($scope.filter.trackDate.start).format('YYYY-MM-DD');
    	$scope.filter.visitTimeEnd = moment($scope.filter.trackDate.end).format('YYYY-MM-DD');
    	$scope.getAppointmentList($scope.curPage);
    };
    // 筛选跟进人
    $scope.selectOwner = function () {
        var owner = 0;
        if(!$scope.select.owner || $scope.select.owner == null) {
            owner = 0;
        } else {
            owner = Tools.parseValue({target: $scope.select.owner, targetValue: $scope.select.owner.userId, type: 'number'});
        }
        $scope.filter.owner = owner;
        
        $scope.getAppointmentList($scope.curPage);
    };
    // 筛选意向班型
    $scope.selectClassTypeId = function() {
    	var classTypeId = 0;
        if(!$scope.select.classTypeId || $scope.select.classTypeId == null) {
            classTypeId = 0;
        } else {
            classTypeId = Tools.parseValue({target: $scope.select.classTypeId, targetValue: $scope.select.classTypeId.id, type: 'number'});
        }
        $scope.filter.classTypeId = classTypeId;
        
        $scope.getAppointmentList($scope.curPage);
    };
    // 筛选状态
    $scope.selectStatus = function() {
    	var status = 0;
        if(!$scope.select.status || $scope.select.status == null) {
            status = 0;
        } else {
            status = Tools.parseValue({target: $scope.select.status, targetValue: $scope.select.status.id, type: 'number'});
        }
        $scope.filter.status = status;
        
        $scope.getAppointmentList($scope.curPage);
    };
    // 搜索
    $scope.search = function() {
    	$scope.getAppointmentList($scope.curPage);
    }


    // 设置初始化参数
    $scope.intiParam = function() {
    	// 设置筛选列表参数
		$scope.filter = {
			classTypeId: 0,
			owner: 0,
			status: 0,
			keyword: "",
			visitTimeStart: "",
			visitTimeEnd: "",
			trackDate: {
				start: "",
				end: ""
			}
		};

		$scope.getAppointmentList(1);
    };
    
    // 新增
    // 打开新增
    $scope.openAdd = function() {
		commonSrv.addAppointmentForm({}, function(result) {
			$scope.form.title = "新增";
			$scope.form.item = {
				eventId: "",
				owner: "",
				status: "",
				inquiryId: [],
				purposeId: "",
				feedback: "",
				notes: "",
				visitTime: "",
				warn: {
					eventId: "",
					feedback: "",
					inquiryId: "",
					notes: "",
					owner: "",
					purposeId: "",
					status: "",
					visitTime: ""
				}
			};
			$scope.form.submit = $scope.addItem;
			$scope.form.eventList = result.data.eventList;
			$scope.form.purposeList = result.data.purposeList;
			$scope.form.statusList = result.data.statusList;
			$scope.form.userList = result.data.userList;
			if($scope.form.userList.length > 0) {
				$scope.form.item.owner = $scope.form.userList[0];
			}
			if($scope.form.statusList.length > 0) {
				$scope.form.item.status = $scope.form.statusList[0];
			}
			$scope.showModel();
		}, function(result) {
			if(result.status == 1) {
				alert(result.msg);
			}
		}, function() {
			$scope.$apply();
		});
    };
    // 新增
    $scope.addItem = function() {
    	var flag = $scope.validate();
		if(!flag) {
			alert("请修改表单");
			return;
		}
		
		// 1.eventId
		var eventId = 0;
        if(!$scope.form.item.eventId || $scope.form.item.eventId == null) {
            eventId = 0;
        } else {
            eventId = Tools.parseValue({target: $scope.form.item.eventId, targetValue: $scope.form.item.eventId.eventId, type: 'number'});
        }
		// 2.inquiryId
    	var inquiryId = 0;
        if(!$scope.form.item.inquiryId || $scope.form.item.inquiryId == null) {
            inquiryId = 0;
        } else {
        	if($scope.form.item.inquiryId instanceof Array) {
        		var temp = $scope.form.item.inquiryId[0];
        		inquiryId = Tools.parseValue({target: temp, targetValue: temp.inquiryId, type: 'number'});
        	}
        }
        // 3.owner
        var owner = 0;
        if(!$scope.form.item.owner || $scope.form.item.owner == null) {
            owner = 0;
        } else {
            owner = Tools.parseValue({target: $scope.form.item.owner, targetValue: $scope.form.item.owner.userId, type: 'number'});
        }
        // 4.visitTime
        var visitTime = "";
        if($scope.form.item.visitTime && $scope.form.item.visitTime instanceof Date) {
    		visitTime = moment($scope.form.item.visitTime).format('YYYY-MM-DD hh:mm');
    	}
        // 5.purposeId
        var purposeId = 0;
        if(!$scope.form.item.purposeId || $scope.form.item.purposeId == null) {
            purposeId = 0;
        } else {
            purposeId = Tools.parseValue({target: $scope.form.item.purposeId, targetValue: $scope.form.item.purposeId.id, type: 'number'});
        }
        // 6.status
        var status = 0;
        if(!$scope.form.item.status || $scope.form.item.status == null) {
            status = 0;
        } else {
            status = Tools.parseValue({target: $scope.form.item.status, targetValue: $scope.form.item.status.id, type: 'number'});
        }
        // 7.feedback
    	var feedback = Tools.parseValue({target: $scope.form.item.feedback, targetValue: $scope.form.item.feedback, type: 'string'});
    	// 8.notes
    	var notes = Tools.parseValue({target: $scope.form.item.notes, targetValue: $scope.form.item.notes, type: 'string'});

    	var data = {
    		eventId: eventId,
			feedback: feedback,
			inquiryId: inquiryId,
			notes: notes,
			owner: owner,
			purposeId: purposeId,
			status: status,
			visitTime: visitTime
    	};
    	commonSrv.addAppointment(data, function(result) {
    		if(result.status == 0) {
    			$scope.msg = "新增成功";
    			$scope.visible = true;
    			$scope.getAppointmentList(1);
    			$scope.hideModel();
    		}
    	}, function(result) {
			if(result.status == 1) {
				alert(result.msg);
			}
		}, function() {
			$scope.$apply();
		});
    };
    // 修改
    // 打开修改
    $scope.openEdit = function(item) {
    	$scope.form.title = "编辑";
		var data = {
			appointmentId: item.appointmentId
		};
		commonSrv.editAppointmentForm(data, function(result) {
			if(result.status == 0) {
				$scope.form.item = result.data.appointment;
				$scope.form.item.warn = {
					eventId: "",
					feedback: "",
					inquiryId: "",
					notes: "",
					owner: "",
					purposeId: "",
					status: "",
					visitTime: ""
				};
				// 设置潜在生
				if(!Tools.reg.isNull($scope.form.item.inquiryId)) {
					if($scope.form.item.inquiry instanceof Array) {
			    		$scope.form.item.inquiryId = $scope.form.item.inquiry;
			    	} else {
			    		$scope.form.item.inquiryId = [$scope.form.item.inquiry];
			    	}
				}
				if(!($scope.form.item.visitTime instanceof Date)) {
					$scope.form.item.visitTime = moment($scope.form.item.visitTime);
				}
				$scope.form.eventList = result.data.eventList;
				$scope.form.purposeList = result.data.purposeList;
				$scope.form.statusList = result.data.statusList;
				$scope.form.userList = result.data.userList;
				// eventId
				$scope.form.item.eventId = Tools.getItemById($scope.form.item.eventId, 'eventId',  $scope.form.eventList);
				// purposeId
				$scope.form.item.purposeId = Tools.getItemById($scope.form.item.purposeId, 'id',  $scope.form.purposeList);
				// status
				$scope.form.item.status = Tools.getItemById($scope.form.item.status, 'id',  $scope.form.statusList);
				// owner
				$scope.form.item.owner = Tools.getItemById($scope.form.item.owner, 'userId',  $scope.form.userList);

				$scope.form.submit = $scope.editItem;
    			$scope.showModel();
    		}
		}, function(result) {
			if(result.status == 1) {
    			alert(result.msg);
    		}
		}, function() {
			$scope.$apply();
		});
    };
    // 修改
    $scope.editItem = function() {
    	var flag = $scope.validate();
		if(!flag) {
			alert("请修改表单");
			return;
		}
		
		// 1.eventId
		var eventId = 0;
        if(!$scope.form.item.eventId || $scope.form.item.eventId == null) {
            eventId = 0;
        } else {
            eventId = Tools.parseValue({target: $scope.form.item.eventId, targetValue: $scope.form.item.eventId.eventId, type: 'number'});
        }
		// 2.inquiryId
    	var inquiryId = 0;
        if(!$scope.form.item.inquiryId || $scope.form.item.inquiryId == null) {
            inquiryId = 0;
        } else {
        	if($scope.form.item.inquiryId instanceof Array) {
        		var temp = $scope.form.item.inquiryId[0];
        		inquiryId = Tools.parseValue({target: temp, targetValue: temp.inquiryId, type: 'number'});
        	}
        }
        // 3.owner
        var owner = 0;
        if(!$scope.form.item.owner || $scope.form.item.owner == null) {
            owner = 0;
        } else {
            owner = Tools.parseValue({target: $scope.form.item.owner, targetValue: $scope.form.item.owner.userId, type: 'number'});
        }
        // 4.visitTime
        var visitTime = "";
        if($scope.form.item.visitTime && $scope.form.item.visitTime instanceof Date) {
    		visitTime = moment($scope.form.item.visitTime).format('YYYY-MM-DD hh:mm');
    	}
        // 5.purposeId
        var purposeId = 0;
        if(!$scope.form.item.purposeId || $scope.form.item.purposeId == null) {
            purposeId = 0;
        } else {
            purposeId = Tools.parseValue({target: $scope.form.item.purposeId, targetValue: $scope.form.item.purposeId.id, type: 'number'});
        }
        // 6.status
        var status = 0;
        if(!$scope.form.item.status || $scope.form.item.status == null) {
            status = 0;
        } else {
            status = Tools.parseValue({target: $scope.form.item.status, targetValue: $scope.form.item.status.id, type: 'number'});
        }
        // 7.feedback
    	var feedback = Tools.parseValue({target: $scope.form.item.feedback, targetValue: $scope.form.item.feedback, type: 'string'});
    	// 8.notes
    	var notes = Tools.parseValue({target: $scope.form.item.notes, targetValue: $scope.form.item.notes, type: 'string'});

    	var data = {
    		eventId: eventId,
			feedback: feedback,
			inquiryId: inquiryId,
			notes: notes,
			owner: owner,
			purposeId: purposeId,
			status: status,
			visitTime: visitTime,
			appointmentId: $scope.form.item.appointmentId
    	};
    	commonSrv.editAppointment(data, function(result) {
    		if(result.status == 0) {
    			$scope.msg = "修改成功";
    			$scope.visible = true;
    			$scope.getAppointmentList($scope.curPage);
    			$scope.hideModel();
    		}
    	}, function(result) {
			if(result.status == 1) {
				alert(result.msg);
			}
		}, function() {
			$scope.$apply();
		});
    };
    // 删除
    // 打开删除
    $scope.openDel = function(item) {
    	$scope.delItem = item;
    	$('#delModel').modal('show');
    };
    // 删除
    $scope.delAppointment = function() {
    	var data = {
			appointmentId: $scope.delItem.appointmentId
		};
		commonSrv.delAppointment(data, function(result) {
			if(result.status == 0) {
				$scope.msg = "删除成功";
				$scope.visible = true;
				$("#delModel").modal('hide');
				$scope.delItem = {};
                $scope.getAppointmentList($scope.curPage);
			}
		}, function(result) {
			if(result.status == 1) {
				alert(result.msg);
			}
			$("#delModel").modal('hide');
		}, function() {
			$scope.$apply();
		});
    };

    // 打开潜在生选择弹层
	$scope.openSelect = function() {
		$scope.form.inquiry = {
			curPage: 1,
			inquiryKeyword: "",
			pageList:  [],
			curPage: 1,
			totalPage: 1,
			totalRow: 0
		};
		$scope.getInquiryList($scope.form.inquiry.curPage, function(result) {
			$("#selectModel").modal({backdrop: 'static', keyboard: false, show: true});
		});
	};
	// 获取潜在生列表
    $scope.getInquiryList = function(curPage, cb) {
    	var data = {
    		channelId: 0,
			curPage: curPage,
			isMy: 0,
			keyword: $scope.form.inquiry.inquiryKeyword,
			pagesize: PAGESIZE,
			searchTraceDateTypeId: 0,	
			status:	0,	
			tagIds: "",	
			traceDateEnd: "",	
			traceDateStart: ""
    	};
    	commonSrv.getInquiryList(data, function(result) {
    		if(result.status == 0) {
    			$scope.form.inquiry.allowSubmit = true;
    			var inquiryList = result.data.inquiryList;
	    		for(var i in inquiryList) {
	    			inquiryList[i].isSelected = false;
	    		}
    			$scope.inquiryList = new NgTableParams({count: PAGESIZE}, { counts: [],  dataset: inquiryList});
    			// 获取分页信息
				var paginate = result.paginate;
				$scope.form.inquiry.totalPage = paginate.totalPage;
				$scope.form.inquiry.totalRow = paginate.totalRow;
				// 设置分页
				$scope.setPage(curPage, 'inquiry');
    			if(typeof cb == 'function') {
    				cb(result);
    			}
    		}
    	}, function(result) {
    		if(result.status == 1) {
    			alert(result.msg);
    		}
    	}, function() {
    		$scope.$apply();
    	});
    };

    // 选择潜在生
    $scope.selectItem = function(item) {
    	for(var i in $scope.inquiryList.data) {
    		$scope.inquiryList.data.isSelected = false;
    	}
    	item.isSelected = true;
    	$scope.form.inquiry.selectedItem = item;
    	$scope.form.inquiry.allowSubmit = false;
    };
    // 确认选择
    $scope.submitSelect = function() {
    	if($scope.form.inquiry.selectedItem instanceof Array) {
    		$scope.form.item.inquiryId = $scope.form.inquiry.selectedItem;
    	} else {
    		$scope.form.item.inquiryId = [$scope.form.inquiry.selectedItem];
    	}
    	$scope.watchInquiryId();
    	$scope.close('#selectModel');
    };
	// 删除已选择潜在生
	$scope.selectedRemove = function() {
		$scope.form.item.inquiryId.pop();
		$scope.watchInquiryId();
	};
    // 关闭二级弹层
    $scope.close = function (name) {
    	Tools.hideModal(name);
    };
    // 搜索
    $scope.searchByInquiryKeyword = function () {
    	$scope.getInquiryList(1);
    };

    // 创建沟通记录
    // 打开创建面板
    $scope.openCreate = function (item) {
    	$scope.traceItem = {
    		notes: "",
    		planNextTrackDate: "",
    		trackDate: "",
    		purpose: "",
    		warn: {
    			notes: "",
	    		planNextTrackDate: "",
	    		trackDate: "",
	    		purpose: ""
    		}
    	};
    	$scope.traceItem.inquiry = item.inquiry;
    	$scope.getTraceList(1, function() {
    		$("#createModel").modal({backdrop: 'static', keyboard: false, show: true});
    	});
    };
    $scope.getTraceList = function(curPage, cb) {
    	var data = {
			curPage: curPage,
			pagesize: PAGESIZE,
			classTypeId: 0,	
			createBy: 0,	
			keyword: "",	
			traceDateEnd: "",
			traceDateStart: "",
			inquiryId: $scope.traceItem.inquiry.inquiryId
		};
		commonSrv.getTraceList(data, function(result) {
			var trackList = result.data.trackList;
			var classTypeList = result.data.classTypeList;
            var userList = result.data.userList;
			for(var i in trackList) {
                // 意向班级
                trackList[i].inquiry.classTypeId = Tools.getItemById(trackList[i].inquiry.classTypeId, 'id', classTypeList);
                // 跟进人
                trackList[i].createBy = Tools.getItemById(trackList[i].createBy, 'userId', userList);
            }
			$scope.traceList = new NgTableParams({count: PAGESIZE}, { counts: [],  dataset: trackList});
			// 跟进人下拉列表
			if($scope.userList.length == 0) {
				$scope.userList = userList;
			}
			// 意向班型下拉列表
			if($scope.classTypeList.length == 0) {
				$scope.classTypeList = classTypeList;
			}
			// 获取分页信息
			var paginate = result.paginate;
			$scope.form.trace.totalPage = paginate.totalPage;
			$scope.form.trace.totalRow = paginate.totalRow;
			// 设置分页
			$scope.setPage(curPage, 'trace');

			// cb
			if(typeof cb == 'function') {
				cb(result);
			}
		}, function(result) {
			if(result.status == 1) {
				alert(result.msg);
			}
		}, function() {
			$scope.$apply();
		});
    };
    // 创建记录
    $scope.createTrace = function() {
    	var flag = $scope.validate('trace');
    	if(!flag) {
    		alert("请修改表单");
    	}
    	var notes = Tools.parseValue({target: $scope.traceItem.notes, targetValue: $scope.traceItem.notes, type: "string"});
    	var purpose = Tools.parseValue({target: $scope.traceItem.purpose, targetValue: $scope.traceItem.purpose, type: "string"});
    	var planNextTrackDate = "";
    	if($scope.traceItem.planNextTrackDate && $scope.traceItem.planNextTrackDate instanceof Date) {
    		planNextTrackDate = moment($scope.traceItem.planNextTrackDate).format('YYYY-MM-DD hh:mm');
    	}
    	var trackDate = "";
    	if($scope.traceItem.trackDate && $scope.traceItem.trackDate instanceof Date) {
    		trackDate = moment($scope.traceItem.trackDate).format('YYYY-MM-DD');
    	} 
    	var data = {
    		notes: notes,
    		planNextTrackDate: planNextTrackDate,
    		trackDate: trackDate,
    		purpose: purpose,
    		inquiryId: $scope.traceItem.inquiry.inquiryId
    	};
    	commonSrv.addTrace(data, function(result) {
    		$scope.traceItem.notes = "";
    		$scope.traceItem.planNextTrackDate = "";
    		$scope.traceItem.trackDate = "";
    		$scope.traceItem.purpose = "";
    		$scope.getTraceList($scope.form.trace.curPage);
    	}, function(result) {
			if(result.status == 1) {
				alert(result.msg);
			}
		}, function() {
			$scope.$apply();
		});
    };


    // 验证
    $scope.watchInquiryId = function () {
		var nullWarn = "不能为空";
        if(Tools.reg.isNull($scope.form.item.inquiryId.length)) {
            $scope.form.item.warn.inquiryId = nullWarn;
        } else {
            $scope.form.item.warn.inquiryId = "";
        }
    };
    $scope.watchOwner = function() {
    	var nullWarn = "请选择";
        if(Tools.reg.isNull($scope.form.item.owner)) {
            $scope.form.item.warn.owner = nullWarn;
        } else {
            $scope.form.item.warn.owner = "";
        }
    }

	$scope.watchTrackDate = function () {
		var nullWarn = "不能为空";
        if(Tools.reg.isNull($scope.traceItem.trackDate)) {
            $scope.traceItem.warn.trackDate = nullWarn;
        } else {
            $scope.traceItem.warn.trackDate = "";
        }
	};
	$scope.watchPlanNextTrackDate = function () {
		var nullWarn = "不能为空";
        if(Tools.reg.isNull($scope.traceItem.planNextTrackDate)) {
            $scope.traceItem.warn.planNextTrackDate = nullWarn;
        } else {
            $scope.traceItem.warn.planNextTrackDate = "";
        }
	};
	$scope.watchNotes = function () {
		var nullWarn = "不能为空";
        if(Tools.reg.isNull($scope.traceItem.notes)) {
            $scope.traceItem.warn.notes = nullWarn;
        } else {
            $scope.traceItem.warn.notes = "";
        }
	};
	$scope.watchPurpose = function () {
		var nullWarn = "不能为空";
        if(Tools.reg.isNull($scope.traceItem.purpose)) {
            $scope.traceItem.warn.purpose = nullWarn;
        } else {
            $scope.traceItem.warn.purpose = "";
        }
	};

	// 初始化
	function init() {
		$scope.intiParam();
		$(document).ready(function() {
			// console.log($rootScope.secondType);
		});
	}
	init();
}]);